class Link extends egret.Sprite {
	private _SafeStr_202: number = 0.5;
	private _SafeStr_203: number = 5;
	private _SafeStr_208: number = 0.4;
	private _SafeStr_227: number = 0.2;
	private m_game: Level;
	// private _SafeStr_201:flash.SoundChannel = null;
	private _SafeStr_204: Anchor = null;
	private _SafeStr_217: Anchor = null;
	private _SafeStr_219: number = 0;
	private _SafeStr_215: number = 0;
	private _SafeStr_209: number = 0;
	private _SafeStr_223: boolean = true;
	private _SafeStr_211: number = 0;
	private _isAttached: boolean = false;
	private _sync: Sync = null;
	private _distance: number = 0;
	private _angle: number = 0;
	private _dest: Cell = null;
	private _SafeStr_222: number = 0;
	private _enabled: boolean = false;
	private _anchor1: Anchor;
	private _anchor2: Anchor;
	private _SafeStr_123: number = 0;
	private _SafeStr_216: number = 0;
	private _SafeStr_125: number = 0;
	private _ogig: Cell = null;
	private linkSegList: Array<LinkSegment>;
	private _SafeStr_221: number = -1;
	private _SafeStr_198: Link = null;
	private _isCut: boolean = false;
	private _SafeStr_200: number = 0;
	private _SafeStr_124: number = 0;
	private _SafeStr_122: number = 0;
	private _isBusy: LinkSegment;
	private _SafeStr_199: number = 0;

	public constructor(game: Level, orig: Cell, dest: Cell) {
		super();

		this.m_game = game;
		this._ogig = orig;
		this._dest = dest;
		this._SafeStr_209 = 0;
		this._isBusy = null;
		this._SafeStr_211 = 0;
		this.linkSegList = new Array();
		this._sync = new Sync(0.5);
		var a1: Anchor = orig.getAnchorTo(dest);
		var a2: Anchor = dest.getAnchorTo(orig);
		this._anchor1 = a1;
		this._anchor2 = a2;
		a1.addListener(this._SafeStr_194.bind(this));
		a2.addListener(this._SafeStr_194.bind(this));
		this._SafeStr_114(a1.x, a1.y, a2.x, a2.y);
		orig.addOriginLink(this);
		orig.addImpulse((orig.getAngleTo(dest) + 180), 1);
		// this._SafeStr_201 = SoundMgr.playSfx(SoundMgr.TENTACLE_GROW);
		this.addEventListener(egret.Event.ENTER_FRAME, this.onEnterFrame, this);
	}

	public static getRequiredPower(len: number): number {
		return (len / (LinkSegment.INTERVAL + LinkSegment.SIZE));
	}

	public cutAtPoint(x: number, y: number) {
		var dist: number = 0;
		if (this._isCut) {
			return;
		}
		this._ogig.delOriginLink(this);
		this._isCut = true;
		if (((this._isAttached) && (!(this._SafeStr_198)))) {
			dist = this._SafeStr_205(this._SafeStr_125, this._SafeStr_124, x, y);
			this._SafeStr_221 = (Math.round((dist / this._SafeStr_199)));
		}
		else {
			this._SafeStr_221 = (this._SafeStr_200);
		}
		this._isAttached = false;
		if (this._SafeStr_198) {
			this._SafeStr_198.setSplitLink(null);
			this.setSplitLink(null);
		}
		// SoundMgr.stopSfx(this._SafeStr_201);
		// this._SafeStr_201 = null;
		// SoundMgr.playSfx(SoundMgr.TENTACLE_CUT);
	}

	private _SafeStr_205(x1: number, y1: number, x2: number, y2: number): number {
		var dx: number = (x2 - x1);
		var dy: number = (y2 - y1);
		return (Math.sqrt(((dx * dx) + (dy * dy))));
	}

	private _SafeStr_206(seg: LinkSegment, pos: number, prevX: number = -1, prevY: number = -1, reversePos: boolean = false) {
		var dispAng: number = (this._angle + (Math.PI / 2));
		var fadePos: number = ((((pos < (this._SafeStr_200 / 2)) ? pos : (this._SafeStr_200 - pos))));
		var dispFade: number = (fadePos * this._SafeStr_208);
		if (dispFade > 1) {
			dispFade = 1;
		}
		var offset: number = this._SafeStr_209;
		var sign: number = 1;
		if (reversePos) {
			offset = (offset + this._distance);
			sign = -1;
		}
		var dispValue: number = ((Math.sin((this._SafeStr_211 + (pos * this._SafeStr_202))) * this._SafeStr_203) * dispFade);
		seg.x = ((this._SafeStr_125 + (Math.cos(this._angle) * (((sign * pos) * this._SafeStr_199) + offset))) + (Math.cos(dispAng) * dispValue));
		seg.y = ((this._SafeStr_124 + (Math.sin(this._angle) * (((sign * pos) * this._SafeStr_199) + offset))) + (Math.sin(dispAng) * dispValue));
		seg.setTendrilSize((dispFade * 8));
		var dir: number = this._angle;
		if (prevX >= 0) {
			dir = Math.atan2((seg.y - prevY), (seg.x - prevX));
		}
		seg.rotation = ((dir * 180) / Math.PI);
	}

	private _SafeStr_212() {
		var splitLen: number = 0;
		var dx: number = (this._SafeStr_204.x - this._anchor1.x);
		var dy: number = (this._SafeStr_204.y - this._anchor1.y);
		var splitX: number = 0;
		var splitY: number = 0;
		if (!this._isAttached) {
			splitLen = ((this.linkSegList.length * this._SafeStr_199) + this._SafeStr_209);
			splitX = (this._SafeStr_125 + (Math.cos(this._SafeStr_215) * splitLen));
			splitY = (this._SafeStr_124 + (Math.sin(this._SafeStr_215) * splitLen));
		}
		else {
			splitX = (this._SafeStr_125 + (dx / 2));
			splitY = (this._SafeStr_124 + (dy / 2));
		}
		if (((splitLen >= (this._SafeStr_216 / 2)) && (!(this._isCut)))) {
			this._isAttached = true;
		}
		this._SafeStr_217.update(splitX, splitY);
	}

	public cutAt(percent: number) {
		if (this._isCut) {
			return;
		}
		this._ogig.delOriginLink(this);
		this._isCut = true;
		if (percent < 0) {
			percent = 0;
		}
		if (percent > 1) {
			percent = 1;
		}
		if (((this._isAttached) && (!(this._SafeStr_198)))) {
			this._SafeStr_221 = (Math.round(((this._distance * percent) / this._SafeStr_199)));
		}
		else {
			this._SafeStr_221 = (this._SafeStr_200);
		}
		this._isAttached = false;
		if (this._SafeStr_198) {
			this._SafeStr_198.setSplitLink(null);
			this.setSplitLink(null);
		}
		// SoundMgr.stopSfx(this._SafeStr_201);
		// this._SafeStr_201 = null;
	}

	public getDestCell(): Cell {
		return (this._dest);
	}

	private _SafeStr_220() {
		var seg: LinkSegment;
		if (((this._isCut) && (this.linkSegList.length > 0))) {
			if (this._SafeStr_221 >= 0) {
				seg = this.linkSegList.shift();
				this.delSeg(seg);
				this._ogig.receiveAttack(this._ogig.type, 1);
				this._SafeStr_221--;
			}
			if ((this._SafeStr_221 + 1) < this.linkSegList.length) {
				seg = this.linkSegList.pop();
				this.delSeg(seg);
				this._dest.receiveAttack(this._ogig.type, 1);
			}
		}
		if (this.linkSegList.length == 0) {
			this.destruct();
		}
	}

	private _SafeStr_114(x1: number, y1: number, x2: number, y2: number) {
		this._SafeStr_125 = x1;
		this._SafeStr_124 = y1;
		this._SafeStr_123 = x2;
		this._SafeStr_122 = y2;
		var dx: number = (x2 - x1);
		var dy: number = (y2 - y1);
		this._angle = Math.atan2(dy, dx);
		this._distance = Math.sqrt(((dx * dx) + (dy * dy)));
		this._SafeStr_222 = (this._distance / (LinkSegment.SIZE + LinkSegment.INTERVAL));
		if (this._SafeStr_222 < 5) {
			this._SafeStr_222 = ((this._SafeStr_222 + 1));
		}
		this._SafeStr_93();
	}

	private _SafeStr_224() {
		var load: number = 0;
		var color: number;
		var seg: LinkSegment;
		var i: number = ((this.linkSegList.length - 1));
		while (i >= 0) {
			seg = this.linkSegList[i];
			load = (seg.load);
			color = (seg.baseColor);
			if (load > 0) {
				seg.setLoad(0);
				if (i < (this.linkSegList.length - 1)) {
					seg = this.linkSegList[(i + 1)];
					seg.setLoad(load);
					seg.baseColor = color;
				}
				else {
					this._dest.receiveAttack(this._ogig.type, load);
				}
			}
			i--;
		}
	}

	private _SafeStr_225(anchor: Anchor) {
		this._SafeStr_204 = this._anchor2;
		this._anchor2.removeListener(this._SafeStr_194.bind(this));
		this._anchor2 = anchor;
		this._anchor2.addListener(this._SafeStr_194.bind(this));
		this._SafeStr_194(anchor);
	}

	public destruct() {

		this._anchor1.removeListener(this._SafeStr_194.bind(this));
		this._anchor2.removeListener(this._SafeStr_194.bind(this));
		if (!this._enabled) {
			this.removeEventListener(egret.Event.ENTER_FRAME, this.onEnterFrame, this);
		}
		this.m_game.delLink(this);
		while (this.linkSegList.length > 0) {
			this.delSeg(this.linkSegList.pop());
		}
	}

	public get power(): number {
		return (this.linkSegList.length);
	}

	private onEnterFrame(event: egret.Event) {
		var seg: LinkSegment;
		this._SafeStr_211 = (this._SafeStr_211 + this._SafeStr_227);
		this._SafeStr_219++;
		if (this._SafeStr_219 > 1) {
			this._SafeStr_219 = (0);
			this._SafeStr_224();
		}
		if (this._isBusy) {
			this._SafeStr_233();
		}
		if (this._SafeStr_217) {
			this._SafeStr_212();
		}
		if ((!(this._SafeStr_217)) || (this._isAttached)) {
			this._SafeStr_223 = (this.linkSegList.length <= this._SafeStr_222);
		}
		if (this._isCut) {
			this._SafeStr_220();
		}
		while (this.linkSegList.length > (this._SafeStr_222 + 1)) {
			seg = this.linkSegList.pop();
			this.delSeg(seg);
			this._ogig.receiveAttack(this._ogig.type, 1);
			this._SafeStr_200 = (this._SafeStr_222);
		}
		this._SafeStr_93();
		this._sync.tick();
		if (((((!(this._isAttached)) && (!(this._isCut))) && (this.linkSegList.length >= (this._SafeStr_222 + 1))) && (this._SafeStr_217 == null))) {
			this._isAttached = true;
			this._dest.addImpulse(this._ogig.getAngleTo(this._dest));
			// SoundMgr.stopSfx(this._SafeStr_201);
			// this._SafeStr_201 = null;
			this.m_game.onLinkAttached(this);
		}
	}

	public getOrigCell(): Cell {
		return (this._ogig);
	}

	public isBusy(): boolean {
		return !(this._isBusy == null);
	}

	public isCut(): boolean {
		return (this._isCut);
	}

	private delSeg(seg: LinkSegment) {
		this.removeChild(seg);
		seg.destruct();
		seg = null;
	}

	private addSegment(): boolean {
		if ((((this._SafeStr_223) && (!(this._isCut))) && (this._isBusy == null))) {
			this._isBusy = new LinkSegment(this._sync, this._ogig.baseColor());
			this._isBusy.x = this._SafeStr_125;
			this._isBusy.y = this._SafeStr_124;
			this._isBusy.rotation = ((this._angle * 180) / Math.PI);
			this._isBusy.setSize(0);
			this._SafeStr_209 = 0;
			this.addChild(this._isBusy);
			return (true);
		}
		return (false);
	}

	private _SafeStr_232(): Anchor {
		this._SafeStr_217 = new Anchor(this._anchor1["x"], this._anchor1["y"]);
		this._SafeStr_204 = this._anchor2;
		this._anchor2 = this._SafeStr_217;
		this._anchor2["addListener"](this._SafeStr_194.bind(this));
		this._SafeStr_194(this._SafeStr_217);
		return (this._SafeStr_217);
	}

	private _SafeStr_194(a: Anchor) {
		var dx: number = 0;
		var dy: number = 0;
		this._SafeStr_114(this._anchor1["x"], this._anchor1["y"], this._anchor2["x"], this._anchor2["y"]);
		if (this._SafeStr_217) {
			dx = (this._SafeStr_204["x"] - this._anchor1["x"]);
			dy = (this._SafeStr_204["y"] - this._anchor1["y"]);
			this._SafeStr_215 = Math.atan2(dy, dx);
			this._SafeStr_216 = Math.sqrt(((dx * dx) + (dy * dy)));
		}
	}

	private _SafeStr_233() {
		var len: number = 0;
		this._SafeStr_209 = (this._SafeStr_209 + 4);
		var div: number = (LinkSegment.SIZE / (LinkSegment.SIZE + LinkSegment.INTERVAL));
		this._isBusy.setSize((this._SafeStr_209 * div));
		if (this._SafeStr_209 >= (LinkSegment.SIZE + LinkSegment.INTERVAL)) {
			this._isBusy.setSize(LinkSegment.SIZE);
			len = (this.linkSegList.unshift(this._isBusy));
			this._isBusy = null;
			this._SafeStr_209 = 0;
			this._SafeStr_200 = (this._SafeStr_222);
		}
	}

	public getDestAnchor(): Anchor {
		return (this._anchor2);
	}

	private _SafeStr_93() {
		if (this._isAttached) {
			this._SafeStr_199 = ((this._distance - this._SafeStr_209) / this._SafeStr_200);
		}
		else {
			this._SafeStr_199 = (LinkSegment.SIZE + LinkSegment.INTERVAL);
		}
		var i: number = 0;
		var prevX: number = -1;
		var prevY: number = -1;
		var seg: LinkSegment;
		if (!this._isCut) {
			i = 0;
			while (i < this.linkSegList.length) {
				seg = this.linkSegList[i];
				this._SafeStr_206(seg, i, prevX, prevY);
				prevX = seg.x;
				prevY = seg.y;
				i++;
			}
		}
		else {
			i = (0);
			while (((i <= this._SafeStr_221) && (i < this.linkSegList.length))) {
				seg = this.linkSegList[i];
				this._SafeStr_206(seg, i, prevX, prevY);
				prevX = seg.x;
				prevY = seg.y;
				i++;
			}
			i = ((this._SafeStr_221 + 1));
			while (i < this.linkSegList.length) {
				seg = this.linkSegList[i];
				this._SafeStr_206(this.linkSegList[i], ((this.linkSegList.length - i) - 1), prevX, prevY, true);
				prevX = seg.x;
				prevY = seg.y;
				i++;
			}
		}
	}

	public setSplitLink(link: Link) {
		this._SafeStr_198 = link;
		if (link != null) {
			link._SafeStr_198 = this;
			this._SafeStr_225(link._SafeStr_232());
		}
		else {
			this._isAttached = false;
			this._anchor2.removeListener(this._SafeStr_194.bind(this));
			this._anchor2 = this._SafeStr_204;
			this._anchor2.addListener(this._SafeStr_194.bind(this));
			this._SafeStr_217 = null;
			this._SafeStr_194(this._anchor2);
		}
	}

	public addSeg(): boolean {
		return (this.addSegment());
	}

	public isAttached(): boolean {
		return (this._isAttached);
	}

	public sendAttack(amount: number): boolean {
		var seg: LinkSegment = this.linkSegList[0];
		if (!seg) {
			return (false);
		}
		if (this._ogig) {
			seg.baseColor = this._ogig.baseColor();
		}
		if (((this._dest) && (this._isAttached))) {
			seg.setLoad(amount);
			return (true);
		}
		return (false);
	}

	public getOrigAnchor(): Anchor {
		return (this._anchor1);
	}

	public pause(enabled: boolean) {
		if (this._enabled == enabled) {
			return;
		}
		this._enabled = enabled;
		if (this._enabled) {
			this.removeEventListener(egret.Event.ENTER_FRAME, this.onEnterFrame, this);
		}
		else {
			this.addEventListener(egret.Event.ENTER_FRAME, this.onEnterFrame, this);
		}
	}

}

